<template>
  <div class="table-box">
    <div class="table-search">
      <el-form :inline="true" :model="form">
        <el-form-item label="游戏分类">
          <el-select v-model="form.category" placeholder="游戏分类" clearable>
            <el-option
              v-for="item in categoryList"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="游戏名称">
          <el-input v-model="form.key_name" placeholder="游戏名称" />
        </el-form-item>
        <el-form-item label="游戏ID">
          <el-input v-model="form.game_id" placeholder="游戏ID" />
        </el-form-item>
        <el-form-item label="游戏类型">
          <el-select v-model="form.game_type" placeholder="游戏类型" clearable>
            <el-option
              v-for="item in gameTypeList"
              :key="item.type"
              :label="item.name"
              :value="item.type"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="时间">
          <el-date-picker
            v-model="form.timeData"
            type="datetimerange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="截止日期"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onQuery">查询</el-button>
          <el-button @click="onReset">重置</el-button>
          <el-button type="primary" plain @click="onAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="card content-box">
      <div class="table">
        <el-table :data="tableData" border v-loading="loading">
          <el-table-column prop="id" label="ID" align="center" />
          <el-table-column
            prop="game_id"
            label="游戏id"
            align="center"
          />
          <el-table-column
            prop="name"
            label="游戏名称"
            align="center"
            width="150"
          />
          <el-table-column label="游戏icon" align="center" width="110">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.icon"
                alt
                fit="fill"
                :preview-src-list="[scope.row.icon]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column label="游戏图片" align="center" width="110">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.img"
                alt
                fit="fill"
                :preview-src-list="[scope.row.img]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column label="说明图片" align="center" width="110">
            <template #default="scope">
              <el-image
                style="width: 70px; height: 70px"
                :src="scope.row.detail_img"
                alt
                fit="fill"
                :preview-src-list="[scope.row.detail_img]"
                :preview-teleported="true"
              ></el-image>
            </template>
          </el-table-column>
          <el-table-column
            prop="slogan"
            label="Slogan"
            align="center"
            width="150"
          />

          <el-table-column
            prop="is_recommend_index"
            label="推荐状态"
            align="center"
            width="120"
          >
            <template #default="scope">
              <el-tag
                :type="scope.row.is_recommend_index ? 'success' : 'danger'"
                >{{
                  scope.row.is_recommend_index ? "已推荐" : "未推荐"
                }}</el-tag
              >
            </template>
          </el-table-column>

          <el-table-column
            prop="is_hot"
            label="是否热门"
            align="center"
            width="120"
          >
            <template #default="scope">
              <el-tag :type="scope.row.is_hot ? 'success' : 'danger'">{{
                scope.row.is_hot ? "以热门" : "未热门"
              }}</el-tag>
            </template>
          </el-table-column>


          <el-table-column
            prop="score"
            label="评分"
            align="center"
          />
          <el-table-column
            prop="online_number"
            label="在线数"
            align="center"
          />


          <el-table-column
            prop="is_hide"
            label="是否隐藏"
            align="center"
            width="120"
          >
            <template #default="scope">
              <el-tag :type="scope.row.is_hide ? 'success' : 'danger'">{{
                scope.row.is_hide ? "已隐藏" : "未隐藏"
              }}</el-tag>
            </template>
          </el-table-column>

          <el-table-column
            prop="is_horizontal"
            label="横屏竖屏"
            align="center"
            width="120"
          >
            <template #default="scope">
              <el-tag :type="scope.row.is_horizontal ? 'success' : 'danger'">{{
                scope.row.is_horizontal ? "横屏" : "竖屏"
              }}</el-tag>
            </template>
          </el-table-column>

          <!-- <el-table-column prop="h5_play_url" label="H5网址" align="center" width="200" /> -->
          <el-table-column
            prop="priority"
            label="排序"
            align="center"
            width="150"
          />
          <!-- <el-table-column prop="createtime" label="添加时间" align="center" :formatter="formatterTime" width="200" /> -->

          <el-table-column
            prop="category"
            label="游戏分类"
            align="center"
          ></el-table-column>
          <!-- <el-table-column prop="version" label="版本" align="center" width="150" /> -->
          <el-table-column
            prop="game_type"
            label="游戏平台"
            align="center"
            :formatter="formatter"
            width="100"
          />

          <el-table-column
            prop="is_recommend_index"
            label="推荐状态"
            align="center"
          >
            <template #default="scope">
              <el-tag
                :type="scope.row.is_recommend_index ? 'success' : 'danger'"
                >{{
                  scope.row.is_recommend_index ? "是" : "否"
                }}</el-tag
              >
            </template>
          </el-table-column>

          <el-table-column
            prop="is_hot"
            label="是否热门"
            align="center"
          >
            <template #default="scope">
              <el-tag :type="scope.row.is_hot ? 'success' : 'danger'">{{
                scope.row.is_hot ? "是" : "否"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="score"
            label="评分"
            align="center"
          />
          <el-table-column
            prop="online_number"
            label="在线数"
            align="center"
          />
          <el-table-column
            prop="is_hide"
            label="是否隐藏"
            align="center"
          >
            <template #default="scope">
              <el-tag :type="scope.row.is_hide ? 'success' : 'danger'">{{
                scope.row.is_hide ? "已隐藏" : "未隐藏"
              }}</el-tag>
            </template>
          </el-table-column>

          <el-table-column
            prop="is_horizontal"
            label="横屏竖屏"
            align="center"
          >
            <template #default="scope">
              <el-tag :type="scope.row.is_horizontal ? 'success' : 'danger'">{{
                scope.row.is_horizontal ? "横屏" : "竖屏"
              }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="priority"
            label="排序"
            align="center"
          />
          <el-table-column
            label="操作"
            width="320"
          align="center"
            fixed="right"
          >
            <template #default="scope">
                <el-button
                  type="primary"
                  plain
                  @click="handleOperate(scope.$index, scope.row, 'details')"
                  >详情</el-button
                >
                <el-button
                  type="primary"
                  plain
                  @click="handleOperate(scope.$index, scope.row, 'edit')"
                  >编辑</el-button
                >
                <el-button
                  type="danger"
                  @click="handleOperate(scope.$index, scope.row, 'delete')"
                  >删除</el-button
                >
                <el-button type="warning" @click="handleSetting(scope.row)"
                  >设置</el-button
                >

            </template>
          </el-table-column>
        </el-table>
        <div class="table-pagination">
          <el-pagination
            :current-page="pageable.pageNum"
            :page-size="pageable.pageSize"
            :page-sizes="[10, 25, 50, 100]"
            :background="true"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageable.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          ></el-pagination>
        </div>
      </div>
    </div>
    <DetailsForm
      @cancel="detailsCancel"
      @closeDialog="detailsCloseDialog"
      :dialogFormVisible="detailsVisible"
      :dialogForm="detailsForm"
    />
    <DialogForm
      @cancel="cancel"
      @closeDialog="closeDialog"
      @submitForm="submit"
      @handleUploaderImgRequest="handleUploader"
      :dialogFormVisible="dialogVisible"
      :dialogForm="dialogForm"
      :btnLoading="btnLoading"
      :categoryList="categoryList"
      :gameTypeList="gameTypeList"
    />
    <SettingForm
      @cancel="settingCancel"
      @closeDialog="settingCloseDialog"
      @submitForm="settingSubmit"
      :dialogFormVisible="settingDialogVisible"
      :dialogForm="settingDialogForm"
      :btnLoading="settingBtnLoading"
    />
  </div>
</template>

<script setup>
import { ref, onMounted, reactive } from "vue";
import { exportToExcel, formatterTime, timestamp } from "@/utils/utils";
import {
  gameListApi,
  gameHideApi,
  gameDelApi,
  gameInfoApi,
  gameAddApi,
  gameUpdateApi,
} from "@/api/moby/game/game";
import DetailsForm from "./components/detailsForm.vue";
import DialogForm from "./components/dialogForm.vue";
import SettingForm from "./components/settingForm.vue";
const tableData = ref([]);
const loading = ref(false);
const detailsVisible = ref(false);
const detailsForm = reactive({});

const dialogVisible = ref(false);
const settingDialogVisible = ref(false);
const settingBtnLoading = ref(false);
const btnLoading = ref(false);

const btnType = ref(null);
const categoryList = ref([]);
const gameTypeList = ref([
  // { type: 0, name: "客户端游戏" },
  // { type: 1, name: "slot游戏" },
  // { type: 2, name: "gamefi游戏" },
  // { type: 3, name: "h5游戏" }
]);
const formData = {
  key_name: "",
  category: "",
  timeData: [],
  game_type: "",
  game_id: "",
};
const form = reactive({ ...formData });
const dialogFormData = {
  game_id: "",
  name: "",
  icon: "",
  icon_show: "",
  img: "",
  img_show: "",
  detail_img: "",
  detail_img_show: "",
  slogan: "",
  category: "",
  version: "",
  version_log: "",
  describe: "",
  h5_download_url: "",
  download_url: "",
  h5_play_url: "",
  game_type: "",
  ios_download_url: "",
  android_download_url: "",
};
const dialogForm = reactive({ ...dialogFormData });

// 设置
const settingDialogFormData = {
  game_id: "",
  is_hide: "",
  is_recommend: "",
  is_hot: "",
  is_horizontal: "",
  priority: "",
  online_number: "",
  score: "",
};
const settingDialogForm = reactive({ ...settingDialogFormData });

const pageable = reactive({
  pageNum: 1,
  pageSize: 10,
  total: 0,
});

const formatter = (row, column, cellValue, index) => {
  console.log(cellValue);
  let name = "";
  gameTypeList.value.filter((item) => {
    if (item.type === cellValue) {
      name = item.name;
    }
  });
  return name;
};
// 设置
const handleSetting = (row) => {
  Object.assign(settingDialogForm, {
    game_id: row.game_id,
    is_hide: row.is_hide ? true : false,
    is_recommend: row.is_recommend_index ? true : false,
    is_hot: row.is_hot ? true : false,
    is_horizontal: row.is_horizontal ? true : false,
    priority: row.priority,
    online_number: row.online_number,
    score: row.score,
  });

  settingDialogVisible.value = true;
};
//关闭弹窗
const settingCloseDialog = () => {
  settingDialogVisible.value = false;
  Object.assign(settingDialogForm, settingDialogFormData);
};

// 取消
const settingCancel = () => {
  settingDialogVisible.value = false;
  Object.assign(settingDialogForm, settingDialogFormData);
};
// 提交
const settingSubmit = async (param) => {
  try {
    settingBtnLoading.value = true;
    // console.log(param);
    const res = await gameHideApi(param);
    ElMessage({
      message: res.message,
      type: "success",
    });

    settingDialogVisible.value = false;
    settingBtnLoading.value = false;
    getTbaleData();
  } catch (error) {
    settingBtnLoading.value = false;
    settingDialogVisible.value = false;
  }
};
// 参数
const param = () => {
  return {
    page: pageable.pageNum,
    pagesize: pageable.pageSize,
    key_name: form.key_name,
    category: form.category,
    game_type: form.game_type,
    start_time: form.timeData[0] ? timestamp(form.timeData[0].valueOf()) : "",
    end_time: form.timeData[1] ? timestamp(form.timeData[1].valueOf()) : "",
    game_id: form.game_id,
  };
};

const handleOperate = async (index, row, type) => {
  // 详情
  if (type == "details") {
    const res = await gameInfoApi({ game_id: row.game_id });
    Object.assign(detailsForm, { ...res.data });
    detailsVisible.value = true;
  }
  //新增
  // 编辑
  if (type === "edit") {
    btnType.value = "edit";
    const res = await gameInfoApi({ game_id: row.game_id });
    Object.assign(dialogForm, { ...res.data });
    dialogVisible.value = true;
  }
  // 删除
  if (type === "delete") {
    ElMessageBox.confirm("您是否确认删除此游戏?", "温馨提示", {
      type: "warning",
    }).then(async () => {
      const res = await gameDelApi({ game_id: row.game_id });
      getTbaleData();
      ElMessage.success(res.message);
    });
  }
};

// // 推荐
// const handleIsRecommend = (type, row = settingRow) => {
//   ElMessageBox.confirm(
//     `您是否确认${type ? "推荐" : "取消推荐"}此游戏?`,
//     "温馨提示",
//     {
//       type: "warning",
//     }
//   ).then(async () => {
//     const res = await gameHideApi({
//       game_id: row.game_id,
//       is_hide: row.is_hide,
//       is_recommend: type,
//     });
//     getTbaleData();
//     ElMessage.success(res.message);
//   });
// };

// //上下架
// const handleIsHide = (type, row = settingRow) => {
//   ElMessageBox.confirm(
//     `您是否确认${type ? "上架" : "下架"}此游戏?`,
//     "温馨提示",
//     {
//       type: "warning",
//     }
//   ).then(async () => {
//     const res = await gameHideApi({
//       game_id: row.game_id,
//       is_hide: type,
//       is_recommend: row.is_recommend_index,
//     });
//     getTbaleData();
//     ElMessage.success(res.message);
//   });
// };
// const closeSettingDialog = () => {
//   settingDialogVisible.value = false;

//   Object.assign(settingRow, {});
// };
// 详情弹窗关闭
const detailsCancel = () => {
  detailsVisible.value = false;

  Object.assign(detailsForm, {});
};
//  详情弹窗关闭
const detailsCloseDialog = () => {
  detailsVisible.value = false;

  Object.assign(detailsForm, {});
};

//新增
const onAdd = () => {
  dialogVisible.value = true;
  btnType.value = "add";
};

//关闭弹窗
const closeDialog = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};

// 取消
const cancel = () => {
  dialogVisible.value = false;
  btnType.value = null;
  Object.assign(dialogForm, dialogFormData);
};
// 提交
const submit = async (param) => {
  try {
    btnLoading.value = true;
    if (btnType.value == "add") {
      // delete param.game_id;
      const res = await gameAddApi(param);

      ElMessage({
        message: res.message,
        type: "success",
      });
    }
    if (btnType.value == "edit") {
      const res = await gameUpdateApi(param);

      ElMessage({
        message: res.message,
        type: "success",
      });
    }

    dialogVisible.value = false;
    btnLoading.value = false;
    getTbaleData();
  } catch (error) {
    btnLoading.value = false;
    dialogVisible.value = false;
  }
};
// //上传图标
const handleUploader = (res, name, src) => {
  if (res.code === 0) {
    ElMessage({
      message: "图片上传成功",
      type: "success",
    });

    dialogForm[src] = res.data.pic_url || "";
    dialogForm[name] = res.data.name || "";
  }
};

// 获取表格数据
const getTbaleData = async () => {
  loading.value = true;

  try {
    const res = await gameListApi(param());
    loading.value = false;
    tableData.value = res.data.results;
    pageable.total = res.data.count;

    categoryList.value = res.data.extra.category.map((item) => {
      return { type: item.value, name: item.name };
    });
    gameTypeList.value = res.data.extra.game_type.map((item) => {
      return { type: item.value, name: item.name };
    });
  } catch (error) {
    loading.value = false;
  }
};
// 重置
const onReset = () => {
  Object.assign(form, formData);
  getTbaleData();
};
// 查询
const onQuery = async () => {
  getTbaleData();
};

// 条数
const handleSizeChange = (number) => {
  pageable.pageSize = number;
  getTbaleData();
};
// 页码
const handleCurrentChange = (number) => {
  pageable.pageNum = number;
  getTbaleData();
};
onMounted(() => {
  getTbaleData();
});
</script>

<style scoped>
.btn-group {
  display: flex;
  justify-content: space-around;
  align-items: center;
  margin-top: 10px;
}
</style>
